package com.motherchild.admin.service.impl;

import com.motherchild.admin.dto.CartDTO;
import com.motherchild.admin.dto.OrderDTO;
import com.motherchild.admin.mapper.OrderMapper;
import com.motherchild.admin.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<OrderDTO> getOrders(int pageNo, int pageSize) {
        int offset = (pageNo - 1) * pageSize;
        return orderMapper.selectOrders(offset, pageSize);
    }

    @Override
    public List<OrderDTO> getUserOrders(int pageNo, int pageSize, Integer userId) {
        int offset = (pageNo - 1) * pageSize;
        return orderMapper.selectUserOrders(offset, pageSize, userId);
    }

    @Override
    public int getTotalOrders() {
        return orderMapper.countOrders();
    }

    @Override
    public void saveOrder(OrderDTO orderDTO) {
        orderMapper.insertOrder(orderDTO);
    }

    @Override
    public void updateOrder(OrderDTO orderDTO) {
        orderMapper.updateOrder(orderDTO);
    }

    @Override
    public void deleteOrder(Integer id) {
        try {
            orderMapper.deleteOrder(id);
            //删除明细表
            orderMapper.deleteOrderDeitem(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public CartDTO getOrderById(Integer id) {
        return orderMapper.selectOrderWithItems(id);
    }
}
